package com.jy.servlet;

import com.jy.bean.Order;
import com.jy.bean.ShoppingTrolley;
import com.jy.bean.User;
import com.jy.service.Orderservice;
import com.jy.service.ShoppingTrolleyService;
import com.jy.service.UserService;
import com.jy.service.impl.OrderSericelmpl;
import com.jy.service.impl.ShoppingTrolleySericelmpl;
import com.jy.service.impl.UserServicelmpl;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.UUID;

public class order_Add_Servlet extends HttpServlet {
    private static final Logger log = LoggerFactory.getLogger(order_Add_Servlet.class);
    private static Orderservice orderservice;
    private static UserService userService;
    private static ShoppingTrolleyService sts;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取到前端传过来的用户账号
        String admin = request.getParameter("user_name");
        //获取到前端传过来的购物车总金额
        String moneys = request.getParameter("money");
        //获取到前端用户输入的姓名
        String consigneeName = request.getParameter("consigneeName");
        //获取到前端用户输入的电话
        String phone = request.getParameter("phone");

        //获取到前端用户输入的地址
        String recordDate = request.getParameter("recordDate");

        orderservice = new OrderSericelmpl();
        userService = new UserServicelmpl();
        sts = new ShoppingTrolleySericelmpl();
        List<User> users = userService.selectByname(admin);

        //获取随机订单
        String UUID = getUUID();
        Integer Strid = 0;
        boolean favg = false;
        boolean fag = false;

        //该订单的总金额
        Integer order_money = Integer.parseInt(moneys);
        for (User user:users){
            Strid = user.getId();
        }
        List<ShoppingTrolley> stList = sts.selectByUserId(Strid);
        log.debug("stList: "+stList);
        for (ShoppingTrolley st:stList){
            sts.deleteById(st.getUser_id());
            orderservice.insertorderdetail(new Order(null,st.getUser_id(),st.getConsignee_id(),st.getPrice(),st.getQuantity(),null,UUID,null,null,null,null,null));
            fag = userService.UpdateUser(new User(st.getUser_id(),null,null,null,recordDate,consigneeName,phone,null));
        }
        favg = orderservice.insertOrder(new Order(null,Strid,null,null,null,null, UUID,null,null,recordDate,consigneeName,phone));
        if (favg&&fag){
            //把随机的订单设置到order_form里面去
            HttpSession session = request.getSession();
            session.setAttribute("order_form",UUID);
            session.setAttribute("order_money",order_money);
            //查询该账号的购物车数量，并给到session里面去
            int Shopping_count = sts.selectByName(admin);
            session.setAttribute("Shopping_count",Shopping_count);
            request.getRequestDispatcher("/orderSuccess.jsp").forward(request,response);
        }else {
            request.getRequestDispatcher("/www.mi.com.jsp").forward(request,response);
        }
    }

    public static String getUUID() {
        String replaceUUID = UUID.randomUUID().toString().replace("-", "");
        return replaceUUID;
    }

//    public static void main(String[] args) {
//        String admin = "331758157432";
//
//        orderservice = new OrderSericelmpl();
//        userService = new UserServicelmpl();
//        sts = new ShoppingTrolleySericelmpl();
//        List<User> users = userService.selectByname(admin);
//
//        //获取随机订单
//        String UUID = getUUID();
//        Integer Strid = 0;
//        for (User user : users) {
//            Strid = user.getId();
//        }
//        List<ShoppingTrolley> stList = sts.selectByUserId(Strid);
//        log.debug("stList: " + stList);
//        for (ShoppingTrolley st : stList) {
//            boolean favg = orderservice.insertOrder(new Order(null, st.getUser_id(), st.getConsignee_id(), st.getPrice(), st.getQuantity(), null, UUID, null, null));
//        }
//    }
}
